package com.econorma.importa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;




import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.econorma.dao.DAO;
import com.econorma.data.JsonData;
import com.econorma.data.LetturaProva;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

 

public class ImportFromJson {
	
	private static Logger logger = Logger.getLogger(ImportFromJson.class);
	private static final String TAG = "ImportFromJson";
	private final DAO dao;
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

	
	public ImportFromJson(DAO dao){
		this.dao=dao;
	}
	
	
	public void importa(){
		

		Gson gson = new Gson();
		
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("json.txt"));
		} catch (FileNotFoundException e) {
			logger.error(TAG, e);
		}
	    
		JsonData[] obj = gson.fromJson(br, JsonData[].class);
		
		dao.delete();
		
		int i = 0;
		for(JsonData j: obj){
			i++;
			LetturaProva lp = new LetturaProva();
			lp.setProva_id(new Long(i));
			lp.setData(j.data);
			lp.setId_sonda(j.id);
			lp.setValore(j.temperatura);
			lp.setUmidita(j.umidita);
			lp.setGroup(9999);
			
			dao.insert(lp);
			System.out.println("Record inserted: " + i);
			
			
 		}
		
 		
	}
	
}
